package ru.spb.osll.myfit.server.parser;

import org.apache.log4j.Logger;
import ru.spb.osll.myfit.common.*;
import ru.spb.osll.myfit.util.StringUtils;
import ru.spb.osll.myfit.wikiverifier.result.TableVerificationResult;

import java.io.File;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;

public class PageUtils {
  private static Logger LOG = Logger.getLogger(PageUtils.class);

  public static String tableToString(Table table) {
    String res = table.isUndecorated() ?  "!" : "";
    
    for (Vector<TableCell> line : table.getData()) {
      res = res + "|" + StringUtils.join(line, "|") + "|" +
          Constants.LINE_SEPARATOR;
    }
    return res;
  }

  public static String includeToString(Include include) {
    String res = TextParsingTools.INCLUDE;
    res = res + include.getPath() + Constants.LINE_SEPARATOR;
    return res;
  }

  public static String contentToString(Content content){
    String res = TextParsingTools.CONTENT;
    res = res + "-R" + content.getLevel() + Constants.LINE_SEPARATOR;
    return res;
  }

  public static void printSuggestion(TableVerificationResult tableResult,
                                     PrintStream stream) {
    stream.println("Error in test table:");
    stream.println(PageUtils.tableToString(tableResult.getTable()));
    stream.println("Suggestion:");
    stream.println(PageUtils.tableToString(tableResult.getSuggestion()));
  }

  public static String defineToString(Define define) {
    String result = "!define " + define.getNameVariable() + " ";
    String value = define.getValueVariable();

    if (value.indexOf('(')==-1) result += "(" + value + ")";
    else if (value.indexOf('[')==-1) result += "[" + value + "]";
    else if (value.indexOf('{')==-1) result += "{" + value + "}";
    else {
      LOG.error("TableUtils: value of " + define.getNameVariable() + " contains '(', '[' and '{' simultaniously (" + value + ")");
      result = "";
    }

    return result;
  }

  public static String pageToString(Page page) {
    String res = "";
    LOG.debug("TableUtils: page elements = " + page.getElements().size());

    for (PageElement pageElement : page.getElements()) {
      
      
      if (pageElement instanceof Include && pageElement.getType() != PageElementType.SET_UP && pageElement.getType() != PageElementType.TEAR_DOWN) {
        LOG.debug("TableUtils: Include");
        res = res + includeToString((Include) pageElement);
        res = res + Constants.LINE_SEPARATOR;
      }

      if (pageElement instanceof Table) {
        LOG.debug("TableUtils: Table");
        res = res + tableToString((Table) pageElement);
        res = res + Constants.LINE_SEPARATOR;
      }

      if (pageElement instanceof WikiText) {
        LOG.debug("TableUtils: WikiText");
        res = res + ((WikiText) pageElement).getWikiText();
        res = res + Constants.TABLE_SEPARATOR;
      }
      
      if (pageElement instanceof Content) {
        res = res + contentToString((Content) pageElement);
        res = res + Constants.LINE_SEPARATOR; 
      }

      if (pageElement instanceof Define) {
        res = res + defineToString((Define) pageElement);
        res = res + Constants.LINE_SEPARATOR;
      }
    }
    return res;
  }
  
  public static String tagsToString(Page page) {
    String result = TextParsingTools.TAGS_PROPERTY_NAME + "=";

    Iterator<PageTag> it = page.getTags().iterator();
    while(it.hasNext()) {
      result += it.next().getString() + (it.hasNext() ? "," : "");
    }
    return result;
  }

  public static String getFullPath(String root, String path) {
    String fullPath = root;
    if (path != null && !path.isEmpty()) {
      path = path.replaceAll(TextParsingTools.ADDRESS_BAR_SEPARATOR_REGEX,
              Matcher.quoteReplacement(File.separator));
      fullPath += File.separator + path;
    }

    return fullPath;
  }

  public static String getFullPathToContent(String root, String path) {
    return getFullPath(root, path) + File.separator + TextParsingTools.DATA_FILE_NAME;
  }
  
  public static String getMetadataPath(String root, String pageName) {
    return getFullPath(root, pageName) + File.separator + TextParsingTools.METADATA_FILE_NAME;
  }
}
